package com.android.lovely.ui.adapter

import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.android.lovely.data.SysMsg
import com.android.lovely.databinding.ItemSysMsgBinding
import com.android.lovely.ext.setSysText
import com.android.lovely.framework.BaseLoadAdapter
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

class SysMsgAdapter : BaseLoadAdapter<SysMsg, SysMsgAdapter.SysHolder>(object : DiffUtil.ItemCallback<SysMsg>() {
    override fun areItemsTheSame(oldItem: SysMsg, newItem: SysMsg): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: SysMsg, newItem: SysMsg): Boolean {
        return oldItem == newItem
    }

}) {

    var onDeleteClick: ((Int) -> Unit)? = null

    var onItemClick: ((SysMsg) -> Unit)? = null

    class SysHolder(parent: ViewGroup, val binding: ItemSysMsgBinding = ItemSysMsgBinding.inflate(parent.layoutInflater, parent, false)) : RecyclerView.ViewHolder(binding.root)

    override fun onBindViewHolder(holder: SysHolder, position: Int) {
        val item = getItem(position) ?: return
        holder.binding.apply {
            unread.isVisible = !item.hasRead
            time.text = item.time
            title.text = item.title
            content.setSysText(item.content)
            root.setOnLongClickListener {
                onDeleteClick?.invoke(item.id)
                true
            }
            root.clickable {
                item.status = 1
                unread.isVisible = false
                onItemClick?.invoke(item)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = SysHolder(parent)

}